import { _decorator, Component, Label, Node, Slider, Sprite, Toggle, Vec2, Vec3 } from 'cc';
import { UIManager } from '../UIManager';
import { LanauageManager } from '../LanauageManager';
import { GameData, rankData } from '../GameData';
import { HttpClient } from '../net/HttpClient';
import { EventManager } from '../EventManager';
import { AudioMgr } from '../AudioMgr';
import { Util } from '../Util';
const { ccclass, property } = _decorator;

@ccclass('rankItem')
export class rankItem extends Component {

    @property(Node)
    rankImg:Node = null;

    @property(Label)
    rankLab:Label = null;

    @property(Sprite)
    avatar:Sprite = null;

    @property(Label)
    nameLab:Label = null;

    @property([Label])
    numLab:Label[] = [];

    rankData:rankData;

    onEnable() {

    }

    refresh(data:rankData, rank:number, isSelf:boolean = false){
        this.rankData = data;
        for (let index = 0; index < this.rankImg.children.length; index++) {
            const element = this.rankImg.children[index];
            if (index >= 3){
                element.active = rank > 3;
            }else{
                element.active = rank == (index+1);
            }
        }
        this.rankLab.string = rank + "";

        let starNums:number[];

        if(isSelf){
            if (rank == 9999){
                this.rankLab.string = "未上榜";
            }
            Util.loadRemoteUrl(GameData.getIns().userData.avatar, this.avatar);

            this.nameLab.string = GameData.getIns().userData.nickname;

            starNums = [GameData.getIns().userData.onestarNum, GameData.getIns().userData.twostarNum, GameData.getIns().userData.threestarNum, GameData.getIns().userData.fourstarNum, GameData.getIns().userData.fivestarNum]
        }else{
            Util.loadRemoteUrl(this.rankData.avatar, this.avatar);

            this.nameLab.string = this.rankData.nickname;

            starNums = [this.rankData.onestarNum, this.rankData.twostarNum, this.rankData.threestarNum, this.rankData.fourstarNum, this.rankData.fivestarNum]
        }

        for (let index = 0; index < this.numLab.length; index++) {
            let element = this.numLab[index];
            element.string = starNums[index] + "";
        }
    }

    onClickHandler(event:Event, str:string){
        if (str == "share"){

        }else if (str == "save"){
            
        }
    }
}

